//
//  LoginAPI.swift
//  SBSafetyApp
//
//  Created by Lina on 2022/12/20.
//

import Foundation
import HandyJSON

// MARK: - 入口-登录
let URLLogin = "/login"      /// 用户登录
let URLUserInfo = "/getInfo" /// 获取用户信息

struct LoginParam: Encodable {
    var code: String?
    var password: String?
    var username: String?
    var uuid: String?
    var timestamp: String?
    var signature: String?
}

let secreKey = "shsgajsyafh5jchz";
let iv = "__jch_syaf5_iv__";

extension API {
    /// 获取用户信息✅已对接
    class func getInfo(withBlock block: ResponseBlock<RespModal>?) {
        let param = getSign()
        NetManager.request(url: URLUserInfo, method: .get, parameters: param, modelType: RespModal.self, resultBlock: block)
    }
    
    /// 用户登录✅已对接
    class func login(withParam param: LoginParam, block: ResponseBlock<LoginRespModal>?) {
        var _param = LoginParam()
        _param.code = param.code
        
        let password = try? AESEncyptUtil.encryptCBC(encryptText: param.password ?? "", key: secreKey, ivs: iv)
        _param.password = password
        _param.username = param.username
        _param.uuid = param.uuid
        
        let signP = getSign(param: _param)
        _param.timestamp = signP.timestamp
        _param.signature = signP.signature
        
        NetManager.request(url: URLLogin, parameters: _param, modelType: LoginRespModal.self, resultBlock: block)
    }
}

struct LoginRespModal: HandyJSON {
    var token: String?
    var role: String?
    var user: UserModal?
    var initEbable: Bool = false
}

struct RespModal: HandyJSON {
    var user: UserModal?
    var data: Any?
    var uuid: String?
    var img: String?
    var roles: [String]?
}

// MARK: - 入口-注册
let URLCommonSetting = "/common/setting" /// 初始化数据
let URLRegisterDepttree = "/register/depttree" /// 网点列表
let URLRegisterPostlist = "/register/postlist" /// 职位列表
let URLRegister = "/register/submit" /// 用户注册

struct RegisterParam: Encodable {
    var username: String?
    var password: String?
    var nickname: String?
    var deptId: String?
    var postId: String?
    var code: String?
    var uuid: String?
    
    var timestamp: String?
    var signature: String?
}

struct DefaultParam: Encodable {
    var timestamp: String?
    var signature: String?
    
    init(timestamp: String? = nil, signature: String? = nil) {
        self.timestamp = timestamp
        self.signature = signature
    }
}

extension API {
    /// 初始化数据
    class func commonSetting(withBlock block: ResponseBlock<RegisterModal>?) {
        let param = getSign()
        NetManager.request(url: URLCommonSetting, method: .get, parameters: param, modelType: RegisterModal.self, resultBlock: block)
    }
    
    /// 网点列表
    class func getRDepttree(withBlock block: ResponseBlock<DeptTreeModal>?) {
        let param = getSign()
        NetManager.request(url: URLRegisterDepttree, method: .get, parameters: param, modelType: DeptTreeModal.self, resultBlock: block)
    }
    
    /// 职位列表
    class func getRPostlist(withBlock block: ResponseBlock<PostModal>?) {
        let param = getSign()
        NetManager.request(url: URLRegisterPostlist, method: .get, parameters: param, modelType: PostModal.self, resultBlock: block)
    }
    
    /// 用户注册
    class func register(withParam _param: RegisterParam, block: ResponseBlock<ResponseData>?) {
        let signP = getSign(param: _param)
        var param = _param
        param.timestamp = signP.timestamp
        param.signature = signP.signature
        NetManager.request(url: URLRegister, parameters: param, modelType: ResponseData.self, resultBlock: block)
    }
}

struct RegisterModal: HandyJSON {
    var registerEnabled: Bool?
    var smsCaptchaEnabled: Bool?
}

struct PostModal: HandyJSON {
    var postId: Int64?
    var postName: String?
}
